package com.zmx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmx.utils.RUtils;
import com.zmx.dao.ScheduleJobDao;
import com.zmx.entity.ScheduleJob;
import com.zmx.service.ScheduleJobService;
import com.zmx.utils.ScheduleUtils;
import com.zmx.utils.SysConstant;
import org.quartz.Scheduler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 定时任务(ScheduleJob)表服务实现类
 *
 * @author makejava
 * @since 2020-10-21 09:43:29
 */
@Service("scheduleJobService")
public class ScheduleJobServiceImpl implements ScheduleJobService {
    @Resource
    ScheduleJobDao scheduleJobDao;
    @Resource
    Scheduler scheduler;
    @Override
    public RUtils scheduleJobList(int limit, int offset, String search, String order) {
        /*动态sql*/
        QueryWrapper<ScheduleJob> queryWrapper = null;
        if (search!=null&&search!=""){
            queryWrapper = new QueryWrapper<>();
            queryWrapper.like("bean_name",search);
        }
        if (order!=null){
            if (queryWrapper == null) {
                queryWrapper = new QueryWrapper<>();
                if ("desc".equals(order)){
                    queryWrapper.orderByDesc("job_id");
                }else {
                    queryWrapper.orderByAsc("job_id");
                }
            }
        }
        /*获得当前页数*/
        long current = offset/limit+1;
        Page<ScheduleJob> page = new Page<>(current,limit);
        IPage<ScheduleJob> scheduleJobIPage = scheduleJobDao.selectPage(page, queryWrapper);
        List<ScheduleJob> records = scheduleJobIPage.getRecords();
        long total = scheduleJobIPage.getTotal();
        return RUtils.success("查询成功",null).put("total",total)
                .put("rows",records);
    }

    @Override
    public RUtils saveScheduleJob(ScheduleJob scheduleJob) {
        scheduleJob.setCreateTime(new Date());
        int insert = scheduleJobDao.insertMy(scheduleJob);
        ScheduleUtils.createTask(scheduler,scheduleJob);
        return insert>0?RUtils.success("插入成功",null):RUtils.fail("插入失败",null);
    }

    @Override
    public RUtils scheduleInfo(long id) {
        ScheduleJob scheduleJob = scheduleJobDao.selectById(id);
        return RUtils.success("成功",null).put("scheduleJob", scheduleJob);
    }

    @Override
    public RUtils updateScheduleJob(ScheduleJob scheduleJob) {
        UpdateWrapper<ScheduleJob> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("job_id",scheduleJob.getJobId());
        int update = scheduleJobDao.update(scheduleJob,updateWrapper);
        ScheduleUtils.updateTask(scheduler,scheduleJob);
        return update>0?RUtils.success("更新成功",null):RUtils.fail("更新失败",null);

    }

    @Override
    public RUtils deleteScheduleJob(List<Long> ids) {
        int i = scheduleJobDao.deleteBatchIds(ids);
        ScheduleUtils.deleteTask(scheduler, ids);
        return i>0?RUtils.success("删除成功",null):RUtils.fail("删除失败",null);

    }

    /**
     *  暂停  status:  0:暂停  1:恢复
     * @param ids
     * @return
     */
    @Override
    public RUtils pauseScheduleJob(List<Long> ids) {
        ScheduleJob scheduleJob = new ScheduleJob();
        scheduleJob.setStatus(SysConstant.JOB_PAUSE.getStatus());
        UpdateWrapper<ScheduleJob> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("job_id",ids);
        int update = scheduleJobDao.update(scheduleJob, updateWrapper);
        ScheduleUtils.pauseTask(scheduler,ids);
        return update>0?RUtils.success("暂停成功",null):RUtils.fail("暂停失败",null);
    }

    @Override
    public RUtils resumeScheduleJob(List<Long> ids) {
        ids.forEach(System.out::println);
        ScheduleJob scheduleJob = new ScheduleJob();
        scheduleJob.setStatus(SysConstant.JOB_RESUME.getStatus());
        UpdateWrapper<ScheduleJob> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("job_id",ids);
        int update = scheduleJobDao.update(scheduleJob, updateWrapper);
        ScheduleUtils.resumeTask(scheduler,ids);
        return update>0?RUtils.success("暂停成功",null):RUtils.fail("暂停失败",null);
    }

    @Override
    public RUtils runOneScheduleJob(List<Long> ids) {
        ScheduleUtils.runOnce(scheduler,ids);
        return RUtils.success("运行成功",null);
    }
}